<?php

namespace app\webapi\vms\service;

use app\webapi\common\BaseService;
use app\webapi\vms\model\HealthModel;
use app\webapi\vms\model\HistoryVisitRecordModel;
use app\webapi\vms\model\VisitRecordModel;

/**
 * @统计服务类
 */
class StatisticsService extends BaseService
{
    /**
     * @desc 获取今日访问统计数据
     * @param $website_id
     * @return array
     * @throws \think\db\exception\DbException
     * @author zhaozhiwei
     * @time 2023/12/14-14:26
     */
    public function getTodayVisitStatisticsDate($website_id)
    {
        $ip_count = VisitRecordModel::where(['website_id' => $website_id])->group('ip')->count();
        $pv_count = VisitRecordModel::where(['website_id' => $website_id])->count();
        $uv_count = VisitRecordModel::where(['website_id' => $website_id])->group(['ip', 'type'])->count();

        return [
            'uv_count' => $uv_count,
            'pv_count' => $pv_count,
            'ip_count' => $ip_count,
        ];
    }

    /**
     * @desc 获取历史访问数据
     * @param $website_id
     * @return mixed
     * @author zhaozhiwei
     * @time 2023/12/14-15:38
     */
    public function getHistoryVisitListByMouth($website_id,$type)
    {
        $limit=7;
        if($type==2){
            $limit=14;
        }
        $list = HistoryVisitRecordModel::field(['uv_count', 'pv_count', 'ip_count', 'date'])
            ->where(['website_id' => $website_id])->limit($limit)->order('date','desc')->select();
        foreach ($list as $key => &$v) {
            $v['id'] = $key+1;
        }
        unset($v);
        return $list;
    }

    /**
     * @desc 获取健康监测数据
     * @param $website_id
     * @return HealthModel[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author zhaozhiwei
     * @time 2023/12/14-15:55
     */
    public function getHealthExaminationList($website_id){
        $list=HealthModel::field(['id','name','residue','unit','is_binding','is_setted'])
            ->append(['is_setted_text','is_binding_text'])->where(['website_id'=>$website_id])->select();
        return $list;
    }
}